<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<style>
div::-webkit-scrollbar {
    display: none;
}
</style>
<div id=scroller style="width: 400px; height: 400px; overflow-y: scroll; will-change: transform; border: 1px solid black">
  <div style="height: 400px"></div>
  <div id=overlap style="backface-visibility: hidden; width: 100px; height: 100px; background-color: red"></div>
  <div id=target style="position: relative; top: -50px; left: 50px; width: 100px; height: 100px; background-color: green"></div>
</div>
<div id="notscrolled" style="position: relative; width: 50px; height: 300px; background: lightgray"></div>
<script>
// Test passes if 'target' (green) is on top of 'overlap' (red).
// The actual test output is a layerization where 'overlap' has a composited layer,
// and 'target' does, but not 'notscrolled'. 'target' has a composited layer because
// it overlaps 'overlap'. 'notscrolled' does not have one because it does not
// intersect the scroller and is outside of it.

function drawsContentAndLayoutObject(layer) {
    if (layer.drawsContent === false)
        return false;
    if (!layer.name.includes("Layout"))
        return false;
    if (layer.name.includes("LayoutView"))
        return false;
    return true;
}

var t = async_test();

onload = function() {
  requestAnimationFrame(function() {
    requestAnimationFrame(function() {
      scroller.scrollTop = 400;
      if (internals) {
        t.step(function() {
          var layers_dump = internals.layerTreeAsText(document);
          var layers = JSON.parse(layers_dump).layers;
          var desiredLayers = layers.filter(drawsContentAndLayoutObject);
          assert_equals(desiredLayers.length, 3, "Actual layers: " + layers_dump);
          assert_true(desiredLayers[0].name.includes("scroller"), "scroller");
          assert_true(desiredLayers[1].name.includes("overlap"), "overlap");
          assert_true(desiredLayers[2].name.includes("target"), "target");
        });
      }
      t.done();
    });
  });
};
</script>
